package com.pb.metadata.controller;

import com.pb.infra.oth.util.ImportExcel;
import com.pb.infra.oth.entity.common.Result;
import com.pb.infra.oth.util.Constant;
import com.pb.infra.oth.util.SysLog;
import com.pb.metadata.service.DataMigrationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 元数据迁移
 * @author liaohaoyuan
 * @date 20210301
 */
@Slf4j
@RestController
@RequestMapping("v1/metadata/dataMigration")
public class DataMigrationController {

    /**
     * 业务逻辑对象
     */
    @Resource
    private DataMigrationService dataMigrationService;

    /**
     * 日志操作表
     */
    private static final String TABLE_NAME = "param_column";

    /**
     * 日志操作功能名
     */
    private static final String OPERATION_MENU = "元数据迁移";

    /**
     * 导出码值信息EXCEL
     *
     * @param paramMap 请求参数
     * @param request  请求参数
     * @throws Exception
     */
    @PostMapping("exportExcel")
    @SysLog(tableName = TABLE_NAME, action = Constant.ACTION_NAME.DOWN, menuName = OPERATION_MENU)
    public Map<String, Object> exportExcel(@RequestBody  Map<String, Object> paramMap, HttpServletRequest request){
        List<Map<String,Object>> dataListSheet1 =  dataMigrationService.selectDataListSheet1(paramMap);
        List<Map<String,Object>> dataListSheet2 =  dataMigrationService.selectDataListSheet2(paramMap);
        //List<Map<String,Object>> dataListSheet3 =  dataMigrationService.selectDataListSheet3(paramMap);
        List<Map<String,Object>> dataListSheet4 =  dataMigrationService.selectDataListSheet4(paramMap);
        List<Map<String,Object>> dataListSheet5 =  dataMigrationService.selectDataListSheet5(paramMap);
        List<Map<String,Object>> dataListSheet6 =  dataMigrationService.selectDataListSheet6(paramMap);
        List<Map<String,Object>> dataListSheet7 =  dataMigrationService.selectDataListSheet7(paramMap);
        List<Map<String,Object>> dataListSheet8 =  dataMigrationService.selectDataListSheet8(paramMap);

        List<Map<String,Object>> tableList=new ArrayList<Map<String,Object>>();
        for(int i=0;i<dataListSheet7.size();i++){
            Map<String,Object> dataMap=dataListSheet7.get(i);
            String tableNameNew=(String)dataMap.get("tableName");
            tableNameNew="pbbase."+tableNameNew;
            boolean exitFlag=dataMigrationService.exitTableByName(tableNameNew);
            if(exitFlag){
                dataMap.put("tableNameNew",tableNameNew);
                tableList.add(dataMap);
            }
        }
        List<Map<String,Object>> dataListSheet9 =  dataMigrationService.selectDataListSheet9(tableList);

        String[][] sheeHeader={{"数据库-标识", "数据库-类型", "数据库-URL", "中文-名称", "有效-标志", "删除-标志", "排序-标志", "备注", "创建-用户", "创建-时间", "更新-用户", "更新-时间", "克隆-标志"},
                {"数据库-用户-标识","数据库-标识", "数据库-用户", "有效-标志", "删除-标志", "排序-标志", "备注", "创建-用户", "创建-时间", "更新-用户", "更新-时间", "克隆-标志"},
                /*{"识别-标识", "中文-名称", "有效-标志", "删除-标志", "排序-标志", "备注", "创建-用户", "创建-时间", "更新-用户", "更新-时间"},*/
                {"字段-标识","数据库-用户-标识","字段-名称","调试-标志","中文-名称","字段-类型","数据-长度","数据-小数","字段-用途",
                        "有效-标志", "删除-标志", "排序-标志", "备注", "创建-用户", "创建-时间", "更新-用户", "更新-时间","记录-状态"},
                {"数据源-标识","数据库-用户-标识", "数据库-对象-名称", "调试-标志","数据源-类型","物理-表-类型","中文-名称","有效-标志",
                        "删除-标志", "排序-标志", "备注", "数据源-用途","历史-数据-类型","记录-状态","创建-用户", "创建-时间", "更新-用户", "更新-时间", "分享-标志"},
                {"数据源-字段-标识","数据源-标识", "字段-标识", "字段-序号", "主键-标志", "可空-标志", "业务键-标志", "描述-标志", "维度-标志",
                        "度量-标志", "源-字段-名称", "源-字段-类型","分区-键-标志","备注","创建-用户","创建-时间","更新-用户","更新-时间"},
                {"码值-表-标识","数据库-用户", "表-名称", "调试-标志", "中文-名称","有效-标志","排序-标志","备注","记录-状态", "创建-用户", "创建-时间", "更新-用户", "更新-时间", "删除-标志"},
                {"码值-类型-标识","中文-名称", "业务-码值-类型", "调试-标志","码值-存放-类型","数据-存放-类型","码值-表-标识",
                        "有效-标志","删除-标志", "排序-标志", "备注","记录-状态", "创建-用户", "创建-时间","更新-用户", "更新-时间","系统-标志"},
                {"码值-标识","码值-类型-标识", "业务-码值", "中文-名称", "上级-标识","层级-标识","叶子-标志","节点-路径","有效-标志","删除-标志", "排序-标志", "备注","相关码值数据表"}};
        String[][] sheetColumn={{"databaseId", "databaseType", "databaseUrl", "cnName", "activeFlag", "deleteFlag", "orderFlag", "remarks", "createUser", "createTime", "updateUser", "updateTime", "cloneFlag"},
                {"databaseUserId","databaseId", "databaseUser", "activeFlag", "deleteFlag", "orderFlag", "remarks", "createUser", "createTime", "updateUser", "updateTime", "cloneFlag"},
                /*{"identifyId", "cnName", "activeFlag", "deleteFlag", "orderFlag", "remarks", "createUser", "createTime", "updateUser", "updateTime"},*/
                {"columnId","databaseUserId","columnName","debugFlag","cnName","columnType","dataLength","dataScale","columnUse",
                        "activeFlag", "deleteFlag", "orderFlag", "remarks", "createUser", "createTime", "updateUser", "updateTime","recordSts"},
                {"datasourceId","databaseUserId", "databaseObjectName", "debugFlag","datasourceType","physicaTableType","cnName","activeFlag",
                        "deleteFlag", "orderFlag", "remarks", "datasourceUse","hisDataType","recordSts", "createUser", "createTime", "updateUser", "updateTime","shareFlag"},
                {"datasourceColumnId","datasourceId", "columnId", "columnNo", "pkFlag", "nullFlag", "bkFlag", "remarkFlag", "dimensionFlag",
                        "metricFlag", "sourceColumnName", "sourceColumnType","patitionKeyFlag","remarks","createUser","createTime","updateUser","updateTime"},
                {"codeTableId","databaseUser", "tableName", "debugFlag", "cnName","activeFlag","orderFlag","remarks", "recordSts", "createUser", "createTime", "updateUser", "updateTime","deleteFlag"},
                {"codeTypeId","cnName", "businessCodeType", "debugFlag","codeStoreType","dataStoreType","codeTableId",
                        "activeFlag","deleteFlag", "orderFlag", "remarks","recordSts", "createUser", "createTime","updateUser", "updateTime","systemFlag"},
                {"codeId","codeTypeId", "businessCode", "cnName", "upperId","levelId","leafFlag","nodePath","activeFlag","deleteFlag", "orderFlag", "remarks","tableNameNew"}};
        String[][] dataType={{"STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING"},
                {"STRING","STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING"},
                /*{"STRING","STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING"},*/
                {"STRING","STRING","STRING","STRING","STRING","STRING","NUMBER","NUMBER","STRING",
                        "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING", "STRING","STRING"},
                {"STRING","STRING", "STRING", "STRING","STRING","STRING","STRING","STRING",
                        "STRING", "STRING", "STRING", "STRING","STRING","STRING", "STRING", "STRING", "STRING", "STRING","STRING"},
                {"STRING","STRING", "STRING", "NUMBER", "STRING", "STRING", "STRING", "STRING", "STRING",
                        "STRING", "STRING", "STRING","STRING","STRING","STRING","STRING","STRING","STRING"},
                {"STRING","STRING", "STRING", "STRING", "STRING","STRING","STRING","STRING", "STRING", "STRING", "STRING", "STRING", "STRING","STRING"},
                {"STRING","STRING", "STRING", "STRING","STRING","STRING","STRING",
                        "STRING","STRING", "STRING", "STRING","STRING", "STRING", "STRING","STRING", "STRING","STRING"},
                {"STRING","STRING", "STRING", "STRING", "STRING","NUMBER","STRING","STRING","STRING","STRING", "STRING", "STRING","STRING"}};
        Object[] sheetData = {dataListSheet1,dataListSheet2,dataListSheet4,dataListSheet5,dataListSheet6,dataListSheet7,dataListSheet8,dataListSheet9};
        String[] sheetName={"param_database","param_database_user","param_app","param_column","param_datasource","param_datasource_column","param_code_table","param_code_type","码值信息数据"};
        paramMap.put("jobBookName",sheetName);
        paramMap.put("title",sheeHeader);
        paramMap.put("order",sheetColumn);
        paramMap.put("data",sheetData);
        paramMap.put("dataType",dataType);//数据类型 有NUMBER,STRING,DOUBLE三种数据类型
        paramMap.put("fileName","业务元数据迁移信息表");
        return  paramMap;
    }

    /**
     * 导入
     * 加入@ImportExcel注解完成数据的校验过程
     * @param paramMap 获取校验完成的数据（data），及提示信息(msg)和状态(caode)，code=1表示正确，code=-1表示数据不正确
     * @return Result 返回数据
     */
    @PostMapping("importExcel")
    public Result importExcel(@ImportExcel() Map<String,Object> paramMap){
        try {
            List<List<Map<String,Object>>> data = ( List<List<Map<String,Object>>>) paramMap.get("data"); //得到Excel中的所有数据
            Integer code= (Integer) paramMap.get("code");//得到校验结果
            if (code!=-1){
                String msg = dataMigrationService.importExcel(data);
                if (!"".equals(msg)){
                    paramMap.put("msg",msg+paramMap.get("msg"));
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            paramMap.put("msg","导入遇到异常请联系管理员");
        }
        paramMap.remove("data");
        return Result.ok(paramMap);
    }
}
